package reflect;

import javafx.util.Pair;

import java.lang.reflect.*;
import java.util.List;

/**
 * @Description
 * @author Jerry
 * @date 2016-08-10
 */
public class MyClass {
    public int count;
    private List<Double> MyList;

    public MyClass(int start) {
        this.count = start;
    }
    public void increase(int step) {
        count = count + step;
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass(0); //一般做法
        myClass.increase(2);
        System.out.println("Normal -> " + myClass.count);
        try {
//            Constructor constructor = MyClass.class.getConstructor(int.class); //获取构造方法
//            MyClass myClassReflect = (MyClass) constructor.newInstance(10); //创建对象
//            Method method = MyClass.class.getMethod("increase", int.class);  //获取方法
//            method.invoke(myClassReflect, 5); //调用方法
//            Field field = MyClass.class.getField("count"); //获取域
//            System.out.println("Reflect -> " + field.getInt(myClassReflect)); //获取域的值


            Field field = MyClass.class.getDeclaredField("MyList"); //myList的类型是List
            Type type = field.getGenericType();
            if (type instanceof ParameterizedType) {
                ParameterizedType paramType = (ParameterizedType) type;
                Type[] actualTypes = paramType.getActualTypeArguments();
                for (Type aType : actualTypes) {
                    if (aType instanceof Class) {
                        Class clz = (Class) aType;
                        System.out.println(clz.getName()); //输出java.lang.String
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
